package client;

import both.Question;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import both.*;
import java.awt.Font;
import java.awt.Toolkit;
import server.GameShowServer;

public class ClientGameTab extends JPanel implements ActionListener {

    private final GameShowClient client;
    private final ArrayList<JButton> buttons = new ArrayList<>();
    private int screenWidth = (int) Game.width;
    private int screenHeight = (int) Game.height;
    private final ArrayList<JLabel> labels = new ArrayList<>();
    private final JLabel questionText;
    private Question currQuestion;

    public ClientGameTab(GameShowClient client) {
        super();
        setLayout(new GridLayout(0, 1, 10, 30));
        setAlignmentX(JPanel.LEFT_ALIGNMENT);
        setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
        questionText = new JLabel(StringFormatter.formatString("Waiting for question...", StringFormatter.TITLE_STR));
        add(questionText);
        this.client = client;
        client.addListener(this);
        client.startListening();
    }

    public void displayQuestion(Question question) {
        buttons.clear();
        labels.clear();
        questionText.setText(StringFormatter.formatString(question.getQuestionText(), StringFormatter.TITLE_STR));
        removeAll();
        add(questionText);

        ArrayList<String> answers = question.getAnswerChoices();

        for (int i = 0; i < answers.size(); i++) {
            JPanel panel = new JPanel();
            JButton button = new JButton(StringFormatter.formatString("Select->", StringFormatter.ANSWER_STR));
            button.setActionCommand("" + i);
            button.addActionListener(this);
            JLabel label = new JLabel(StringFormatter.formatString(answers.get(i), StringFormatter.QUESTION_STR));
            buttons.add(button);
            labels.add(label);
            panel.add(button);
            panel.add(label);
            add(panel);
        }

        currQuestion = question;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String choice = e.getActionCommand();
        if (choice.equals("new-question")) {
            displayQuestion((Question) e.getSource());
        } else {
            int choiceIndex = Integer.parseInt(choice);
            for (int i = 0; i < buttons.size(); i++) {
                if (i != choiceIndex) {
                    labels.get(i).setForeground(Color.gray);
                    buttons.get(i).setEnabled(false);
                }
            }
            Answer answer = new Answer();
            answer.setAnswerText(currQuestion.getAnswerChoices().get(choiceIndex));
            answer.setQuestionText(currQuestion.getQuestionText());
            client.sendAnswer(answer);
        }

    }
}
